//
//  TravelHelper.m
//  Travelmemory
//
//  Created by lanou3g on 16/3/23.
//  Copyright © 2016年 ABCD. All rights reserved.
//

#import "TravelHelper.h"
#import "AFNetworking.h"
@implementation TravelHelper

//  单例
+(TravelHelper *)defaultTravelHelper{
    
    static TravelHelper *travelHelper = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        travelHelper = [[TravelHelper alloc] init];
    });
    return travelHelper;
    
}

//  网络请求
//  请求数据接口
-(void)get:(NSString *)url params:(NSDictionary *)params success:(void(^)(id responseObj)) success failure:(void(^)(NSError *error))failure{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html",nil];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            
            //            NSLog(@"%@",responseObject);
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            
            failure(error);
        }
        
    }];
}


- (void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    AFHTTPRequestOperationManager *managerPost = [AFHTTPRequestOperationManager manager];
    managerPost.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", nil];
    
    [managerPost.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    [managerPost POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if (success) {
            //            NSLog(@"%@",responseObject);
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if (failure) {
            
            
            failure(error);
        }
        
    }];
    
}

-(void)sendReportInfo:(NSString *)url andData:(NSData *)data{
    
    NSURL *urlData = [NSURL URLWithString:url];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlData cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSInputStream *inputStream = [NSInputStream inputStreamWithData:data];
    [request setHTTPBodyStream:inputStream];
    __weak TravelHelper *http = self;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data == nil) {
            NSLog(@"------------------------%@",connectionError);
            return;
        }
        else{
            http.backBlock(data);
        }
    }];
    
    
    
}
- (void)sendReportInfo:(NSString *)url andData:(NSData *)data backData:(void (^)(NSData *))backData{
    
    NSURL *urlData = [NSURL URLWithString:url];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlData cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSInputStream *inputStream = [NSInputStream inputStreamWithData:data];
    [request setHTTPBodyStream:inputStream];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data == nil) {
            NSLog(@"----%@",connectionError);
            return;
        }
        else{
            backData(data);
        }
    }];
    
    
}


@end
